1 using System;
2 using
UnityEngine;
3 using
UnityStandardAssets.CrossPlatformInput;
4
5 namespace
UnityStandardAssets.Vehicles.Ball
6 {
7     
public class BallUserControl : MonoBehaviour
8     {
9         
private Ball ball; // Reference to the ball controller.
10
11         
private Vector3 move;
12         
// the world-relative desired move direction, calculated from the camForward and user input.
13
14         
private Transform cam; // A reference to the main camera in the scenes transform
15         
private Vector3 camForward; // The current forward direction of the camera
16         
private bool jump; // whether the jump button is currently pressed
17
18
19         
private void Awake()
20         {
21             
// Set up the reference.
22             ball = GetComponent<Ball>();
23
24
25             
// get the transform of the main camera
26             
if (Camera.main != null)
27             {
28                 cam = Camera.main.transform;
29             }
30             
else
31             {
32                 Debug.LogWarning(
33                     
"Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
34                 
// we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
35             }
36         }
37
38
39         
private void Update()
40         {
41             
// Get the axis and jump input.
42
43             
float h = CrossPlatformInputManager.GetAxis("Horizontal");
44             
float v = CrossPlatformInputManager.GetAxis("Vertical");
45             jump = CrossPlatformInputManager.GetButton(
"Jump");
46
47             
// calculate move direction
48             
if (cam != null)
49             {
50                 
// calculate camera relative direction to move:
51                 camForward = Vector3.Scale(cam.forward,
new Vector3(1, 0, 1)).normalized;
52                 move = (v*camForward + h*cam.right).normalized;
53             }
54             
else
55             {
56                 
// we use world-relative directions in the case of no main camera
57                 move = (v*Vector3.forward + h*Vector3.right).normalized;
58             }
59         }
60
61
62         
private void FixedUpdate()
63         {
64             
// Call the Move function of the ball controller
65             ball.Move(move, jump);
66             jump =
false;
67         }
68     }
69 }


Gõ tìm kiếm nhanh...